package com.javasm.crm.service.sell.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.entity.sell.SellOrder;
import com.javasm.crm.domin.entity.sell.SellOrderGifts;
import com.javasm.crm.domin.entity.sell.SellOrderGoods;
import com.javasm.crm.domin.vo.SellOrderDetailVO;
import com.javasm.crm.mapper.sell.SellOrderGiftsMapper;
import com.javasm.crm.mapper.sell.SellOrderGoodsMapper;
import com.javasm.crm.mapper.sell.SellOrderMapper;
import com.javasm.crm.service.sell.ISellOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqz
 * @since 2021-07-12
 */
@Service
@Transactional
public class SellOrderServiceImpl extends ServiceImpl<SellOrderMapper, SellOrder> implements ISellOrderService {

    @Autowired
    private SellOrderMapper sellOrderMapper;

    @Autowired
    private SellOrderGoodsMapper sellOrderGoodsMapper;

    @Autowired
    private SellOrderGiftsMapper sellOrderGiftsMapper;



    @Override
    public boolean addSellOrder(SellOrderDetailVO sellOrderDetailVO) {
        SellOrder sellOrder = new SellOrder();
        sellOrder.setSellCode(sellOrderDetailVO.getSellCode());
        sellOrder.setOrderType(sellOrderDetailVO.getOrderType());
        sellOrder.setOrderAction(sellOrderDetailVO.getOrderAction());
        sellOrder.setBusinessType(sellOrderDetailVO.getBusinessType());
        sellOrder.setOrderCreateTime(sellOrderDetailVO.getOrderCreateTime());
//        sellOrder.setRefuseTime(sellOrderDetailVO.getRefuseTime());
//        sellOrder.setRefuseReason(sellOrderDetailVO.getRefuseReason());
        sellOrder.setConsigneeName(sellOrderDetailVO.getConsigneeName());
        sellOrder.setMemberAccount(sellOrderDetailVO.getMemberAccount());
        sellOrder.setPhoneNumber(sellOrderDetailVO.getPhoneNumber());
        sellOrder.setCellphoneNumber(sellOrderDetailVO.getCellphoneNumber());
        sellOrder.setPostalCode(sellOrderDetailVO.getPostalCode());
        sellOrder.setDeliveryAddress(sellOrderDetailVO.getDeliveryAddress());
        sellOrder.setDeliveryArea(sellOrderDetailVO.getDeliveryArea());
        sellOrder.setDeliveryWay(sellOrderDetailVO.getDeliveryWay());
        sellOrder.setDeliveryCost(sellOrderDetailVO.getDeliveryCost());
        sellOrder.setPayWay(sellOrderDetailVO.getPayWay());
//        sellOrder.setSelfPickupTime(sellOrderDetailVO.getSelfPickupTime());
        sellOrder.setEmail(sellOrderDetailVO.getEmail());
        sellOrder.setPayer(sellOrderDetailVO.getPayer());
        sellOrder.setRemark(sellOrderDetailVO.getRemark());
        int i = sellOrderMapper.insert(sellOrder);

        List<SellOrderGoods> sellOrderGoodsList = sellOrderDetailVO.getSellOrderGoodsList();
        List<SellOrderGifts> sellOrderGiftsList = sellOrderDetailVO.getSellOrderGiftsList();

        if (!sellOrderGoodsList.isEmpty()){
            for (SellOrderGoods sellOrderGoods : sellOrderGoodsList) {
                int j = sellOrderGoodsMapper.insert(sellOrderGoods);
                if (j <= 0){
                    return false;
                }
            }
        }

        if (null != sellOrderGiftsList || sellOrderGiftsList.size() != 0){
            for (SellOrderGifts sellOrderGifts : sellOrderGiftsList) {
                int k = sellOrderGiftsMapper.insert(sellOrderGifts);
                if (k <= 0){
                    return false;
                }
            }
        }

        return i > 0;
    }

    @Override
    public SellOrder searchOne(QueryWrapper<SellOrder> sellOrderQueryWrapper) {
        return sellOrderMapper.selectOne(sellOrderQueryWrapper);
    }


}
